
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import com.constant.*;
import com.obscure.delib.*;


interface GUI_Map
{
	public final static int backbutton = 1000;	
	public final static int button1    = 1001;
}

public class ScreenStateMap implements ScreenState {

	private SpriteBatch m_spriteBatch;
    private deSprite m_splashDeSprite;
    private deSprite m_button;
    
    private GuiElement[] m_gbutton = new GuiElement[9];
    private GuiElement m_gbackbutton;
    private boolean m_isStateDone=false;   
    private final BitmapFont m_font;
    
	public int m_NextState=0;
	public ScreenStateMap(Application app) {
		// TODO Auto-generated constructor stub

		m_spriteBatch    = new SpriteBatch();
		m_splashDeSprite = new deSprite();
		m_splashDeSprite.setSptiteBatch(m_spriteBatch);
		m_splashDeSprite.loadData(DATA_SPRITE.SPR_BGMAIN);
		
		m_button = new deSprite();
		m_button.setSptiteBatch(m_spriteBatch);
		m_button.loadData(DATA_SPRITE.SPR_BUTTON);
		createGUI();
		m_NextState =GameState.STATE_GP;
		
		m_font = new BitmapFont(Gdx.files.internal("xdata/fonts/font16.fnt"), Gdx.files.internal("xdata/fonts/font16.png"), false);
	}

	private void createGUI()
	{		
		int offsetX =80;
		int offsetY =192;
		for(int i=0;i<m_gbutton.length;i++)
		{
			int a = i / 3; 
			int b = i % 3;
			m_gbutton[i] = new GuiElement(m_button);
			m_gbutton[i].setPos(offsetX+(210*a), GameConstant.H_SCREEN - (offsetY+60*(b)));
			m_gbutton[i].setAnim(SPRITE_BUTTON.ANIM_0);
			m_gbutton[i].setGUIWidthHight(195,42);
			m_gbutton[i].setGuiID(GUI_Map.button1+i);
			
			GuiElementManager.getInstance().addElement(m_gbutton[i]);
		}
		// back button
		m_gbackbutton = new GuiElement(m_button);
		m_gbackbutton.setPos(695, 80);
		m_gbackbutton.setAnim(SPRITE_BUTTON.BACK_5);
		m_gbackbutton.setGUIWidthHight(70,52);
		m_gbackbutton.setGuiID(GUI_Map.backbutton);
		GuiElementManager.getInstance().addElement(m_gbackbutton);

	}
	@Override
	public void update(Application app) {
		int clickedGUI = GuiElementManager.getInstance().update(Gdx.input);
		if(clickedGUI != -1) {
			executeClickedButton(clickedGUI);
		}
	}

	private void executeClickedButton(int clickedGUI) {
		if(clickedGUI == GUI_Map.backbutton)
		{
				System.out.println("--button1 clicked-- ");
				m_NextState =GameState.STATE_MM;
				m_isStateDone=true;
		}
		else if(clickedGUI >= GUI_Map.backbutton)
		{
			if(clickedGUI - GUI_Map.backbutton <= 2)//available map = 2
			{
				m_NextState =GameState.STATE_GP + (clickedGUI - GUI_Map.backbutton-1);
				m_isStateDone=true;
			}
			System.out.println("--level-- :"+(clickedGUI - GUI_Map.backbutton-1));
		}
		
	}

	@Override
	public void render(Application app) {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   //clear screen
        Gdx.gl.glClearColor(255, 255, 255, 255);    //fill screen with white color
		// bg map
		m_splashDeSprite.paintFrame(0, 0, 0);
		// all button
		GuiElementManager.getInstance().render();
	}

	@Override
	public boolean isDone() {
		return m_isStateDone;
	}

	@Override
	public void dispose() {
		for(int i=0;i<m_gbutton.length;i++)
		{	
			GuiElementManager.getInstance().removeElement(m_gbutton[i]);
		}
		GuiElementManager.getInstance().removeElement(m_gbackbutton);
		m_spriteBatch.dispose();
		m_splashDeSprite.dispose();
		
	}
	
	public int getNextState() {
		return m_NextState;
	}

}
